home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / GETRSRCL.C < prev    next >
C/C++ Source or Header  |  1990-08-12  |  5KB  |  180 lines

  1. /********************************/
  2. /* File: GetRsrcList.c            */
  3. /*                                */
  4. /* Given the name of a file,      */
  5. /* return a list of all the     */
  6. /* resources in that file.  The */
  7. /* list contains 1 line per     */
  8. /* resource with the following  */
  9. /* format:                        */
  10. /*                                 */
  11. /* item1 == Resource Type        */
  12. /* item2 == Resource ID            */
  13. /* item3 == Resource name        */
  14. /*                                 */
  15. /* If the resource fork is empty*/
  16. /* or non-existent, return empty*/
  17. /*                                */
  18. /* ----------------------------    */
  19. /* ⌐1990 Donald Koscheka        */
  20. /* All Rights Reserved            */
  21. /********************************/
  22.  
  23. #define     UsingHypercard
  24.  
  25. #include    <MacTypes.h>
  26. #include    <OSUtil.h>
  27. #include    <MemoryMgr.h>
  28. #include    <FileMgr.h>
  29. #include    <ResourceMgr.h>
  30. #include    <pascal.h>
  31. #include    <string.h>
  32. #include    <hfs.h>
  33. #include     "HyperXCmd.h"
  34. #include    "HyperUtils.h"
  35.  
  36. #define        nil        0L
  37.  
  38. /*** definition of resource header ***/
  39. typedef struct {
  40.     long    r_data_offset;
  41.     long    r_map_offset;
  42.     long    r_data_size;
  43.     long    r_map_size;
  44.     char    r_reserved[112];
  45.     char    r_application[128];
  46. } resHdr, *resHdrPtr, **resHdrHand;
  47.  
  48.  
  49. /*** definition of resource map ***/
  50. typedef struct {
  51.     long        r_header[4];        /* duplicates first 16 bytes in header */
  52.     long        next_map;
  53.     short        f_ref;
  54.     short        attributes;
  55.     short        r_type_list;
  56.     short        r_name_list;
  57. } resMap, *resMapPtr, **resMapHand;
  58.  
  59.  
  60. /*** definition of type list entry ***/
  61. typedef struct {
  62.     char        r_type[4];        /*** this resource type                 ***/
  63.     short        r_count;        /*** number of resources of this type    ***/
  64.     short        r_ref;            /*** offset from start of type list to    ***/
  65.                                 /*** reference list for resources of     ***/
  66.                                 /*** this type (not used here)            ***/
  67. } resTyp, *resTypPtr, **resTypHand;
  68.  
  69.  
  70.  
  71. /*** definition of resource type list ***/
  72. typedef struct {
  73.     short    r_count;        /* 0..n */
  74.     resTyp    r_typ[];        /* array of type entries     */
  75. } resTypeList, *resTypeListPtr, **resTypeListHand;
  76.  
  77.  
  78.  
  79.  
  80. /*** definition of resource reference list ***/
  81. typedef struct {
  82.     short    r_id;            
  83.     short    r_name;            /* offset from start of name list to this name    */
  84.     long    r_offset;        /* offset to date (hi byte == attributes         */
  85.     Handle    r_handle;        /* handle when resource is in memory            */
  86. } resRef, *resRefPtr, **resRefHand;
  87.  
  88.  
  89.  
  90.  
  91.  
  92. pascal void main( paramPtr )
  93.     XCmdBlockPtr    paramPtr;
  94. {
  95.     resMapPtr        rMap    = NIL;
  96.     resTypeListPtr    rTL;
  97.     resTypPtr        rTyp;
  98.     resRefPtr        rRef;
  99.     Handle            rsrcList = NIL; /* the output                */
  100.     resHdr            rHdr;
  101.     Str31            fName;            /* name of the file            */
  102.     short            ref;
  103.     OSErr            err;
  104.     long            temp;
  105.     short            i,j;            /* loop counters            */
  106.     Str255            str;
  107.     
  108.     paramPtr->returnValue = NIL;    /* prepare for the worst     */
  109.                                     /* (but plan for the best)    */
  110.     
  111.     if( paramPtr->params[0] ){
  112.  
  113.         HLock( paramPtr->params[0] );
  114.         ZeroToPas( paramPtr, *(paramPtr->params[0]), &fName );
  115.         HUnlock( paramPtr->params[0] );
  116.  
  117.         err  = (short)OpenRF( &fName, -1, &ref );
  118.  
  119.         if( !err )         /*** Move to  start of  file and read header ***/
  120.             err = SetFPos( ref, fsFromStart, 0L );
  121.         else
  122.             return;
  123.             
  124.         if( !err ){        /*** read in the resource header     ***/
  125.             temp = (long)sizeof( resHdr );
  126.             err = FSRead( ref, &temp, &rHdr );
  127.         }
  128.         
  129.         if( !err )        /*** Move to the start of the map and read it in ***/
  130.             err = SetFPos( ref, fsFromStart, rHdr.r_map_offset );
  131.         
  132.         if( !err ){        
  133.             rMap = (resMapPtr)NewPtr( rHdr.r_map_size );
  134.             err = FSRead( ref, &rHdr.r_map_size, rMap);
  135.         }
  136.         
  137.         if( !err ){
  138.             /*** this calculation doesn't seem correct ***/
  139.             rTL = (resTypeListPtr)((long)rMap + (long)(rMap->r_type_list));
  140.  
  141.             /*** move to the start of the reference list ***/
  142.             rTyp = (resTypPtr)((long)rTL + (long)sizeof( short )); 
  143.             
  144.             /* Allocate handle for the output list    */
  145.             if( rsrcList = NewHandle( 0L ) ){
  146.                 for( i = 0; i <= rTL->r_count; i++ ){
  147.                     rRef = (resRefPtr)((long)rTL + (long)rTyp->r_ref );
  148.                     for( j = 0; j <= rTyp->r_count; j++ ){
  149.                         /*** read the reference list entry int     ***/
  150.                         
  151.                         /*** add this type & id to the output list ***/
  152.                         AppendCharToHandle( rsrcList, (char)rTyp->r_type[0]);
  153.                         AppendCharToHandle( rsrcList, (char)rTyp->r_type[1]);
  154.                         AppendCharToHandle( rsrcList, (char)rTyp->r_type[2]);
  155.                         AppendCharToHandle( rsrcList, (char)rTyp->r_type[3]);
  156.                         AppendCharToHandle( rsrcList, ',');
  157.                         
  158.                         /*** convert the id to a string and add to list ***/
  159.                         NumToStr( paramPtr, (long)rRef->r_id, &str );
  160.                         pStrToField( (char *)str, '\r',  rsrcList );
  161.                         rRef++;
  162.                     }/* for j = 0 to the number of resources of this type */
  163.                     rTyp++; 
  164.                 }/* for i = 0 to the number of resource types */
  165.                 
  166.             }/* if( rsrcList allocated */
  167.             
  168.             AppendCharToHandle( rsrcList, '\0' );
  169.         }                
  170.         
  171.         if( rMap )
  172.             DisposPtr( (Ptr)rMap );
  173.         /*** Only reach here if file was opened    ***/
  174.         err = FSClose( ref );
  175.     }
  176.     
  177.     paramPtr->returnValue = rsrcList;
  178. }
  179.  
  180.